package com.car.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.car.cache.AsyncImageLoader;
import com.car.entity.NewsItem;
import com.car.util.ConstantsUtils;
import com.car.util.DisplayUtil;
import com.example.car.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

public class NewsItemAdapter extends SimpleBaseAdapter<NewsItem> {
	private ListView listView;

	public NewsItemAdapter(Context c, List<NewsItem> datas, ListView listView) {
		super(c, datas);
		this.listView = listView;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		EntityHolder entityHolder = null;
		if (convertView == null) {
			entityHolder = new EntityHolder();
			convertView = layoutInflater.inflate(R.layout.news_item, null);
			ViewUtils.inject(entityHolder, convertView);
			convertView.setTag(entityHolder);
		} else {
			entityHolder = (EntityHolder) convertView.getTag();
		}
		entityHolder.item_tv_title.setText(datas.get(position).title);
		entityHolder.item_iv_img.setTag(ConstantsUtils.SERVER_IMG_URL
				+ datas.get(position).cover_image);
		AsyncImageLoader.getInstance(c)
				.loadBitmap(
						listView,
						entityHolder.item_iv_img,
						ConstantsUtils.SERVER_IMG_URL
								+ datas.get(position).cover_image,
						DisplayUtil.dip2px(c, 105), DisplayUtil.dip2px(c, 70));

		return convertView;
	}

	private class EntityHolder {
		@ViewInject(R.id.item_iv_img)
		ImageView item_iv_img;
		@ViewInject(R.id.item_tv_title)
		TextView item_tv_title;
	}

}
